//编写子函数，实现upload
#include"../include/p.h"

int s_Upload(int newfd,char *filename)
{
	char buf[100]={0};
	//1.得到文件大小
	struct stat statbuff;
	printf("%s",filename);
	if(-1==stat(filename,&statbuff))
	{
		printf("stat error ");
		return -1;
	}
	else 
	{
		int filesize=statbuff.st_size;
		printf("stat ok the file size is %d\n",filesize);
	}
	int filesize=statbuff.st_size;
	//2.将文件大小转为字符串存入buf中
	sprintf(buf,"%d",filesize);
	//粘包？
	//3.文件大小传入客户端
	send(newfd,buf,strlen(buf),0);
	printf("文件大小上传成功\n");
	//清空buf
	memset(buf,'\0',sizeof(buf));

	//接收
	recv(newfd,buf,sizeof(buf),0);
	printf("客户端是否ok？%s",buf);
	//打开文件进行传输
	int fr=open(filename,O_RDONLY);
	if(fr<0)
	{
		perror("open error");
		return -1;
	}
	while(1)
	{
		memset(buf,'\0',sizeof(buf));
		int n=read(fr,buf,sizeof(buf));
		if(0==n)
		{
			close(fr);
			break;
		}
		send(newfd,buf,n,0);

	}
	printf("文件上传成功！\n");
	return 0;
}
